package hasthi.common;

import java.io.FileWriter;
import java.io.StringReader;
import java.io.Writer;
import java.net.InetAddress;

import org.xmlpull.v1.builder.XmlDocument;
import org.xmlpull.v1.builder.XmlElement;

//import wsmg.NotificationHandler;
//import wsmg.WseClientAPI;
import xsul.XmlConstants;
import xsul.soap.SoapUtil;
import xsul.soap11_util.Soap11Util;
import xsul.soap12_util.Soap12Util;

public class PerfDataCollecter {
//public class PerfDataCollecter implements NotificationHandler {
    private Writer out;
    public PerfDataCollecter() throws Exception{
        out = new FileWriter("perf-"+System.currentTimeMillis());
    }
    
    
    public void handleNotification(String event) {
        try {
            SoapUtil[] soaputils = new SoapUtil[]{Soap11Util.getInstance(),Soap12Util.getInstance()};
            XmlDocument envelope = XmlConstants.BUILDER.parseReader(new StringReader(event));
            SoapUtil soapUtil = SoapUtil.selectSoapFragrance(envelope, soaputils);
            XmlElement notifcation = soapUtil.requiredBodyContent(envelope);
            System.out.println(notifcation.requiredTextContent());
            out.write(notifcation.requiredTextContent()+"\n");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
    public void listen() throws Exception{
//        WseClientAPI client = new WseClientAPI();
//        int port = 34677;
//        String brokerUrl = "http://tyr11.cs.indiana.edu:12346/";
//        client.startConsumerService(port,this );
//        String consumerUrl = "http://" + InetAddress.getLocalHost().getCanonicalHostName() + ":"
//                + port;
//        String subcriptionId = client.subscribe(brokerUrl, consumerUrl, "ADMIN");
//        System.in.read();
//        client.unSubscribe(brokerUrl, subcriptionId);
//        client.shutdownConsumerService();
//        out.close();
    }

    public static void main(String[] args) throws Exception {
        new PerfDataCollecter().listen();
    }
}
